#include <QDebug>
#include <QThread>
#include "workthread.h"

WorkThread::WorkThread(QObject *parent): QThread(parent)
{
    runFlag = true;
    pauseFlag = true;
}



void WorkThread::run()
{
    QString fileFullName;
    QStringList info;
    VideoCapture capture;
    Mat frame;
    ::IdentifyModule::BboxSeq objKind;
    int res =0;

    if(cp == FPGA)
        iceClient = new FpgaClient();
    else if(cp == DK200)
        iceClient = new Dk200Client();
    else if(cp == ARM)
        iceClient = new ArmClient();
    else if(cp == NVIDIA)
        iceClient = new NvidiaGpuClient();

    if(imageType == PICTURE){
        imageBase = new PictureImage();
        imageBase->setSource(directoryName);
    }
    else if(imageType == VIDEO){
        imageBase = new VideoImage();
        imageBase->setSource(videoFile);
    }
    res = iceClient->request(processType,info);
    //while(runFlag){
        Mat image;
        // 图片集合

        while (imageBase->getCvMat(image) == 0) {
            if(!runFlag )
                break;
            while((!pauseFlag)){
                QThread::sleep(1);
            }

            fileFullName = imageBase->getSource();
            res = iceClient->request(processType,image,objKind);
            if(res != 0)
                continue;
            QString msgRes;
            msgRes.append(QString("%1,").arg(objKind.size()));
            for (int i = 0; i < objKind.size(); i++)
            {
                msgRes.append(QString("%1,%2,%3,%4,%5,").arg(QString::fromStdString(objKind[i].name))
                              .arg(objKind[i].rect.left).arg(objKind[i].rect.top)
                        .arg(objKind[i].rect.right).arg(objKind[i].rect.bottom));
            }

            if(processType == 1 || processType == 3){//200dk detect or fpga detect
                emit send_output_text(QString("%1,%2,%3,%4,%5,%6").arg("detect").arg(fileFullName)
                                      .arg(info[0]).arg(info[1]).arg(iceClient->getRequestTimeSpan()).arg(msgRes),threadName);
            }else if( processType == 2){//200dk classify
                emit send_output_text(QString("%1,%2,%3,%4,%5,%6").arg("classify").arg(fileFullName)
                                      .arg(info[0]).arg(info[1]).arg(iceClient->getRequestTimeSpan()).arg(msgRes),threadName);
            }



            if(imageType == PICTURE){
                //QThread::msleep(1000);
            }

        }

    //}
    delete iceClient;
    delete imageBase;
    qDebug() << "[info] work thread stop";

}

void WorkThread::setFlag(bool flag, int flagType)
{
    switch (flagType) {
        case 1:
            runFlag = flag;
            break;
        case 2:
            pauseFlag = flag;
            break;
    }

}

void WorkThread::setFlag(bool flag, int flagType, QString threadName)
{

    if(threadName != this->threadName)
        return;
    switch (flagType) {
        case 1:
            runFlag = flag;
            break;
        case 2:
            pauseFlag = flag;
            break;
    }

}

void WorkThread::setProcessType(int param){
    processType = param;
}

void WorkThread::setThreadName(QString param)
{
    threadName = param;
}

void WorkThread::setVideoFile(QString param)
{
    videoFile = param;
}

void WorkThread::setComputingPlatform(ComputingPlatform param)
{
    cp = param;
}

void WorkThread::setImageType(ImageSourceType param)
{
    imageType = param;
}

void WorkThread::clearFileNameList()
{
    fileNameList.clear();
}
void WorkThread::setDirectoryName(QString arg)
{
    directoryName = arg;
}

void WorkThread::setFileNameList(QStringList arg)
{
    fileNameList = arg;
}
